import Store from '@/lib/store';
import Utils from '@/lib/utils';
import Config from '@/lib/config';
import Http from '@/lib/http';
import urlMap from './urlMap';

export class Lib {
	constructor() {
		this.$store = Store; // 数据存储
		this.$utils = Utils; // 工具
		this.$http = Http; // 请求
		this.$config = Config // 配置
		this.$urlMap = urlMap // 请求地址

		this.$http.interceptor = {
			before: this.http_before.bind(this),
			after: null
		};
	}
	static getInstance() {
		if (!this.instance) {
			this.instance = new Lib()
		}
		return this.instance
	}


	/**
	 * 发送请求前的回调
	 * @param {Object} _config - 配置
	 * */
	http_before(_config) {
		// 该请求是否需要登录
		if (_config.needLogin) {
			if (!this.checkIfLogin()) {
				return false;
			}
			let userinfo = this.getUserinfo();
			let _token = userinfo.token;

			if (_token) {
				_config['header']['token'] = _token;

				if (_config.url.indexOf('?') === -1) {
					_config.url =
						`${_config.url}?token=${_token}`;
				} else {
					_config.url = _config.url.replace('?',
						`?token=${_token}&`);
				}

				if (_config.data.length == 0) {
					_config.data = `token=${_token}`;
				} else {
					_config.data = `${_config.data}&token=${_token}`;
				}
			} else {
				return false;
			}
		}

		return _config;
	}
	/**
	 * 检查是否登录
	 * @param {object} objs
	 * @returns {boolean} 如果登录状态正常，如果正常，返回true，否则返回false
	 */
	async checkIfLogin() {
		let userinfo = this.getUserinfo();
		// 检查是否登录
		// ①：检查用户数据是否存在
		// ②：检查token是否过期
		if (userinfo) {
			if (userinfo.token) {
				// 判断token是否过期
				let now = Date.parse(new Date());

				if (userinfo.expiretime === 0 || userinfo.expiretime * 1000 > now) {
					return true; // 登录态正常
				}
			}
		}
		// 退出登录
		this.logout();

		return false; // 登录失效
	}
	/**
	 * 获取当前用户信息
	 * @returns {object} 如果登录状态正常返回用户信息，否则返回空对象{}
	 */
	getUserinfo() {
		let userinfo = this.$store.getters.user_Info;
		if (userinfo) {
			if (userinfo.token) {
				// 判断token是否过期
				let now = Date.parse(new Date());
				if (userinfo.expiretime === 0 || userinfo.expiretime * 1000 > now) {
					return userinfo; // 登录态正常
				}
			}
		}
		// 退出登录
		this.logout();
		return {};
	}
	/**
	 * 退出登录
	 * @returns {boolean} 如果退出登录成功返回true，否则返回false
	 */
	async logout() {
		this.$store.commit('clearUserInfo');
		return true
	}
	runTimer() {
		// #ifdef MP-WEIXIN
		this.getNoticeNum();
		this.sendLocation();
		
		if (this.$store.getters.user_Info.token != undefined && getApp().globalData.timer == null) {
			getApp().globalData.timer = setInterval(() => {
				this.getNoticeNum();
				this.sendLocation();
			}, 3000)
		}
		// #endif
	}

	async getNoticeNum() {
		let res = await Http.post({
			url: urlMap.unreadNum,
			data: {},
			needLogin: true
		})
		if (res.code == 1) {
			if (res.data > 0) {
				Store.commit('SETTABBARBADGE', res.data.toString())
				uni.setTabBarBadge({
					index: 3,
					text: res.data.toString()
				})
			} else {
				uni.removeTabBarBadge({
					index:3
				})
			}
		}
	}
	async sendLocation() {
		if(this.$store.getters.user_location.lat != undefined){
			let {lat,lng} = this.$store.getters.user_location
			let res = await Http.post({
				url: urlMap.sendLocation,
				data: {
					lat: lat,
					lng: lng
				},
				needLogin: true
			})
		}
	}
}

let _myLib = Lib.getInstance();
export default _myLib;